package com.njtoyo.taxi.admin.library.mail;

import cn.hutool.core.util.IdUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @Author Dell
 * @Date 2021/6/2 9:13
 */
@Component
@Slf4j
public class SpringMailSender {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private TemplateEngine templateEngine;

    @Value("${spring.mail.username}")
    private String mailFrom;

    @SneakyThrows
    public void sendTemplateMail(String mailTo){
        Context context = new Context();
        String text = IdUtil.simpleUUID();
        context.setVariable("project", "demo");
        context.setVariable("author", "njtoyo");
        context.setVariable("code", text);
        String emailContent = templateEngine.process("test.html", context);
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(mailFrom);
        helper.setTo(mailTo);
        helper.setSubject("标题");
        helper.setText(emailContent, true);
        helper.addAttachment("fileToSend.xlsx",new File("fileToSend"));
        javaMailSender.send(message);
    }

    public void sendSimpleMail(String mailTo){
        SimpleMailMessage message = new SimpleMailMessage();

        message.setSubject("标题");
        message.setText("测试发送");

        message.setFrom(mailFrom);
        message.setTo(mailTo);

        javaMailSender.send(message);
    }

    @Async
    public void sendAuthorizationCodeMail(AuthorizationCodeMailRequest request) throws MessagingException {
        log.info("正在发送授权码邮件 {}", request);
        Context context = new Context();
        context.setVariable("operationUserName", request.getSensitiveOperationHelper().getOperationUser().getName());
        context.setVariable("operationName", request.getSensitiveOperationHelper().getOperation().getOperationDesc());
        context.setVariable("authorizationCode", request.getCode());
        context.setVariable("expireDate", request.getExpireDate().toString());
        String emailContent = templateEngine.process("authorizationCodeMail.html", context);
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(mailFrom);
        helper.setTo(request.getMailTo());
        helper.setSubject(String.format("操作授权 - %s", request.getSensitiveOperationHelper().getOperation().getOperationDesc()));
        helper.setText(emailContent, true);
        javaMailSender.send(message);
        log.info("邮件发送成功");
    }

}
